package Dao;

import java.sql.*;
import java.util.ArrayList;

public class ShopWay {
    public ArrayList<Shop> getAllData(){
        ArrayList<Shop> shopList=new ArrayList<>();
        Connection con;
        String sql;
        ResultSet resultSet;
        PreparedStatement preparedStatement;
        try {
            boolean could_register=false;
            Class.forName("com.mysql.jdbc.Driver");
            con= DriverManager.getConnection("jdbc:mysql://localhost:3306/it?useUnicode=true&characterEncoding=utf8","root","123456");
            sql="select * from shop";
            preparedStatement=con.prepareStatement(sql);
            resultSet=preparedStatement.executeQuery();
            while(resultSet.next()){
                String linshi_name=resultSet.getString("name");
                int linshi_number=resultSet.getInt("number");
                double linshi_price=resultSet.getDouble("price");
                int linshi_id=resultSet.getInt("id");
                Shop shop=new Shop();
                shop.setPrice(linshi_price);
                shop.setName(linshi_name);
                shop.setNumber(linshi_number);
                shop.setId(linshi_id);
                shopList.add(shop);
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return shopList;
    }
    public ArrayList<Shop> getAll(String name){
        ArrayList<Shop> shopList=new ArrayList<>();
        Connection connection;
        String sql,shop = null;
        PreparedStatement preparedStatement;
        ResultSet resultSet;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/it?useUnicode=true&characterEncoding=utf8","root","123456");
            sql="select * from login where name= ?";

            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,name);
            System.out.println("展示购物车名称");
            resultSet=preparedStatement.executeQuery();
            while (resultSet.next()){
               shop=resultSet.getString("buy_name");
                System.out.println("shop的值"+shop);
            }
            String linshi_shop[]=shop.split("\\*");
            for(int i=0;i<linshi_shop.length;i++){
                String Two_linshi_shop[]=linshi_shop[i].split(",");
                Shop shop1=new Shop();
                System.out.print(Two_linshi_shop[0]+"\t");
                System.out.print(Two_linshi_shop[1]+"\t");
                System.out.println(Two_linshi_shop[2]+"\t");
                shop1.setName(Two_linshi_shop[0]);
                shop1.setPrice(Double.parseDouble(Two_linshi_shop[1]));
                shop1.setNumber(Integer.parseInt(Two_linshi_shop[2]));
                shopList.add(shop1);
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return shopList;
    }
    public double getAllPrice(String name){
        double price=0;
        Connection connection;
        String sql;
        PreparedStatement preparedStatement;
        ResultSet resultSet;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/it?useUnicode=true&characterEncoding=utf8","root","123456");
            sql="select * from login where name= ?";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,name);
            resultSet=preparedStatement.executeQuery();
            while (resultSet.next()){
                double linshi_price=resultSet.getDouble("zong_price");
                price=linshi_price;
                break;
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return price;
    }
}
